/*
Ultimate MMORPG Kit - GameScripts/AI - Client's script - skeletarik@gmail.com - 2013
This script is for enemies. It contains simple AI. You can choose type of the enemy: agressive or not. 
If it is agressive, enemy will attack the Player when he enters in his trigger. You can edit the radius in the inspector.
Enemy has two different attacks: usual hit and critical hit. You can edit the possibility of the critical hit in the inspector (<attack2chance> in the Server!).
If you don't want to use the critical hit, set <attack2chance> to 0. 
For correct work you need to set 4 animations clips. 
Also you can set enemy's rotation, moving and attacking speed in the inspector (the server's values MUST be equal to the client's values!).

NB! It is the client's version of the script. The Server's version of the script also exists. You can find it in the Server folders.
*/

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

	public bool aggressive;			//Will enemy attack at once when player enters in his trigger?
	public AnimationClip hit;		//Animation clip for usual hit
	public AnimationClip critialHit;//Animation clip for critical hit
	public AnimationClip die;		//Animation clip for die
	public AnimationClip getHit;	//Animation clip for getHit
	public AnimationClip run;		//Animation clip for run
	public AnimationClip idle;		//Animation clip for idle
	
	public Transform enemy;				//Automatically-detected target. 
	
	public int rotationSpeed=1;		//Enemy's rotation speed
	public int movingSpeed=1;		//Enemy's moving speed
	public float attackSpeed = 1.0f;	//Enemy's attacking speed
	public float defautlAttackSpeed = 1.0f;
	public float attackDistance = 0.5f;
	
	public bool isAttacking;//Is enemy attacking?
	public int  criticalAttackRatio = 20;					//Usual or critical hit

	public void Awake()
	{

	}
		
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Hero" || other.gameObject.tag == "Buddy" )
		{
			if(aggressive==true){
				enemy = other.gameObject.transform;
				isAttacking=true;
			}
		}
	}
	
	void OnTriggerExit(Collider other)
	{	
		if(enemy!=null)
		{
			if(other.gameObject.name == enemy.gameObject.name)
			{
				isAttacking=false;
				enemy = null;
			}
		}
	}

	void OnCollisionEnter(Collision  other)
	{	
		if(other.gameObject.tag == "Bullet")
		{
			CancelInvoke();
			animation.clip = getHit;
			animation.Play( );
			Invoke("StopAttack",animation[getHit.name].time);
		}
	}

	void StopAttack(){
		isAttacking=false;	
		animation.clip = idle;
		animation.Play( );
	}

	void AttackAgain(){
		isAttacking=true;	
		animation.clip = hit;
		animation.Play( );
	}
		
	void DoAttack(){
		if(animation.clip.name!="die")
		{
			if(enemy!=null){

				//Attacking only if the enemy is looking at the player. In other way the enemy will run or turn to the player
				if(Mathf.Abs(transform.position.x - enemy.transform.position.x) <= attackDistance
				   && Mathf.Abs(transform.position.z-enemy.transform.position.z) <= attackDistance)
				{
					if(attackSpeed > 0)
					{
						attackSpeed -= Time.deltaTime;			//Timer
					}
					if(attackSpeed <= 0)
					{						
						int r = Random.Range(0,100);

						if(criticalAttackRatio > r)
						{		
							gameObject.animation.clip = critialHit;	//There the enemy uses usual hit or critical hit. 
						}
						else
						{
							gameObject.animation.clip = hit;
						}
						gameObject.animation.Play();
						animation.PlayQueued(idle.name,QueueMode.CompleteOthers);
						attackSpeed = defautlAttackSpeed;
						Debug.LogError("Attack");
					}
				}
				else
				{											//If the enemy isn't looking at the player, the enemy will run or turn to the player
					animation.clip = run;
					animation.Play();
					transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(enemy.transform.position - transform.position), rotationSpeed * Time.deltaTime);
					transform.position += transform.forward * movingSpeed * Time.deltaTime;
				}
			}
			else
			{
				StopAttack();
			}
		}
	}

	void Update()
	{
		if(isAttacking)
		{
			DoAttack();
		}
	}
	
	IEnumerator Ability(){
		isAttacking=false;
		yield return new WaitForSeconds(0);
		isAttacking=true;
	}
}